/**
 * Date: 2022/1/4
 * Author: january
 * Description: 
 */
#include <libecc/cipher/cipher_algs.h>

static const cipher_mapping cipher_maps[] = {
#ifdef WITH_CIPHER_AES_CBC
    {.type = AES_CBC,	/* AES_CBC */
	 .name = "aes_cbc",
	 .block_size = AES_BLOCK_SIZE,
	 .enc_init = (_cipher_func_enc_init)aes_encrypt_init,
     .dec_init = (_cipher_func_dec_init)aes_decrypt_init,
	 .enc = (_cipher_func_enc)aes_encrypt_cbc,
     .dec = (_cipher_func_dec)aes_decrypt_cbc,
	 .enc_finalize = (_cipher_func_enc_finalize)aes_finalize,
	 .dec_finalize = (_cipher_func_dec_finalize)aes_finalize},
#endif
#ifdef WITH_CIPHER_AES_CTR
    {.type = AES_CTR,	/* AES_CBC */
    .name = "aes_ctr",
    .block_size = AES_BLOCK_SIZE,
    .enc_init = (_cipher_func_enc_init)aes_encrypt_init,
    .dec_init = (_cipher_func_dec_init)aes_encrypt_init,
    .enc = (_cipher_func_enc) aes_process_ctr,
    .dec = (_cipher_func_dec) aes_process_ctr,
    .enc_finalize = (_cipher_func_enc_finalize)aes_finalize,
    .dec_finalize = (_cipher_func_dec_finalize)aes_finalize},
#endif
#ifdef WITH_CIPHER_SM4_CBC
   {.type = SM4_CBC,	/* AES_CBC */
    .name = "sm4_cbc",
    .block_size = SM4_BLOCK_SIZE,
    .enc_init = (_cipher_func_enc_init)sm4_init,
    .dec_init = (_cipher_func_dec_init)sm4_init,
    .enc = (_cipher_func_enc)sm4_encrypt_cbc,
    .dec = (_cipher_func_dec)sm4_decrypt_cbc,
    .enc_finalize = (_cipher_func_enc_finalize)sm4_finalize,
    .dec_finalize = (_cipher_func_dec_finalize)sm4_finalize},
#endif
#ifdef WITH_CIPHER_SM4_CTR
    {.type = SM4_CTR,	/* AES_CBC */
    .name = "sm4_ctr",
    .block_size = SM4_BLOCK_SIZE,
    .enc_init = (_cipher_func_enc_init)sm4_init,
    .dec_init = (_cipher_func_dec_init)sm4_init,
    .enc = (_cipher_func_enc) sm4_process_ctr,
    .dec = (_cipher_func_dec) sm4_process_ctr,
    .enc_finalize = (_cipher_func_enc_finalize)sm4_finalize,
    .dec_finalize = (_cipher_func_dec_finalize)sm4_finalize},
#endif
};

const cipher_mapping *get_cipher_by_name(const char *cipher_name)
{
    const cipher_mapping *m = NULL, *ret = NULL;
    u8 i;

    for (i = 0, m = &cipher_maps[i]; m->type != UNKNOWN_CIPHER_ALG;
         m = &cipher_maps[++i]) {
        const char *exp_name = (const char *)m->name;

        if (are_str_equal(cipher_name, exp_name)) {
            ret = m;
            break;
        }
    }

    return ret;
}

const cipher_mapping *get_cipher_by_type(cipher_alg_type cipher_type) {
    const cipher_mapping *m = NULL, *ret = NULL;
    u8 i;

    for (i = 0, m = &cipher_maps[i]; m->type != UNKNOWN_CIPHER_ALG;
         m = &cipher_maps[++i]) {
        if (m->type == cipher_type) {
            ret = m;
            break;
        }
    }

    return ret;
}

